﻿var Url = "AjaxData/CustomOperating.aspx";
var vname = "";
//自动义双击方法
function CustomDbClick() {
    Check(1);
}
var Check = function (obj) {
    var recordID = myDataTable.getSelectedRows()[0];
    record = myDataTable.getRecord(recordID);
    vname = record.getData("CustomerName");
    var vid = record.getData(const_entityID);
    if (vid != "") {
        window.parent.$("#tabs").tabs('close', '查看客户信息');
        var pagetitle = "查看客户信息";
        parent.addTabs(pagetitle, '../Crm/Customer/AddCustomer.aspx?r=' + Math.random() + '&back=' + obj + '&type=0&flag=0&entity=CrmCustomer&id=' + vid + '&title=' + encodeURI(pagetitle));
        parent.NeedRefreshPage = this;
        var vContent = "点击查看客户信息";
        trySaveButtonDatLog(vContent, vid);
    }
}
//自定义新增方法
var fnCustomAdd = function () {
    //检查录入客户数的设置数量
    var SaveUrl = Url + "?r=" + Math.random() + "&action=check&entity=CrmCustomerCheck&json=&id=0";
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "1") {
                    var vContent = "点击新增客户信息";
                    trySaveButtonDatLog(vContent, "00000000-0000-0000-0000-000000000000");
                    reloadData();  //跳转至公海添加页面
                } else if (str == "2") {
                    alert("您今天录入的客户总数已超过设置的客户总数，如有疑问请联系管理员!");
                    return;
                } else {
                    alert("数据异常!");
                    return;
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}
var reloadData = function () {
    window.parent.$("#tabs").tabs('close', '新增客户信息');
    var pagetitle = "新增客户信息";
    parent.addTabs(pagetitle, '../Crm/Customer/AddCustomer.aspx?r=' + Math.random() + '&type=0&flag=1&entity=CrmCustomer&title=' + encodeURI(pagetitle));
    parent.NeedRefreshPage = this;
}
//自定义修改方法
var fnCustomEdit = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    var SaveUrl = Url + "?r=" + Math.random() + "&action=check&entity=CrmCustomerCheck&json=&id=" + vid;
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "1") {
                    var vContent = "点击修改客户信息";
                    trySaveButtonDatLog(vContent, vid);
                    fnModify0(vid);
                } else {
                    alert(str);
                    return;
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}
var fnModify0 = function (vid) {
    window.parent.$("#tabs").tabs('close', '修改客户信息');
    var pagetitle = "修改客户信息";
    parent.addTabs(pagetitle, '../Crm/Customer/AddCustomer.aspx?r=' + Math.random() + '&type=0&flag=2&entity=CrmCustomer&id=' + vid + '&title=' + encodeURI(pagetitle));
    //parent.NeedRefreshPage = this;
}

//自定义删除方法
var fnCustomDeleteData = function () {
    //entity = CrmCustomer;
    SaveUrl = "AjaxData/DataSave.aspx?r=" + Math.random() + "&action=delete&entity=CrmCustomer&id=" + record.getData(const_entityID);
   // alert(SaveUrl);
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "1") {
                    alert("数据删除成功");
                    var recordID = mDataTable.getSelectedRows()[0];
                    if (dl.length == 0) {
                        mDataTable.deleteRow(recordID);
                    }
                    else {
                        for (var i = 0; i < dl.length; i++) {
                            mDataTable.deleteRow(dl[i]);
                        }
                    }
                }
                else {
                    alert(str + "\n" + "数据删除失败!");
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
    YAHOO.example.container.dialog3.hide();
}
//转为私海
var fnPrivateCustomer = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("是否确定将该客户转为私海?")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=procedure&entity=CrmPrivateCustom&json=&id=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("客户公海转私海成功!");
                        } else {
                            alert(str);
                        }
                        //RefreshData();
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}
//转为意向客户
var fnSCustomer = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("是否确定将该客户转为意向客户?")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=procedure&entity=CrmWillCustomer&json=&id=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("转为意向客户成功!");
                            //fnCustomerItems();
                        } else{
                            alert(str);
                        }
                        //RefreshData();
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}
//意向客户列表
function fnCustomerItems() {
    window.parent.$("#tabs").tabs('close', '意向客户列表');
    var pagetitle = "意向客户列表";
    parent.addTabs(pagetitle, '../Extend/YUIList.aspx?entity=V_CrmWillCustomer&order=Code&functionID=57da4052-4bd9-4b6a-a953-9ae9a0e15244');
    parent.NeedRefreshPage = this;
}
//转为公海
var fnPublicCustomer = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("是否确定将该客户转为公海?")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=procedure&entity=CrmCustomerPublic&json=&cid=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("转为公海客户成功!");
                        } else {
                            alert(str);
                        }
                        //RefreshData();
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}
//停用客户
var fnStopCustomer = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("您确定将该客户停用吗?停用后公海中将无法显示该客户信息！")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=delete&entity=CrmCustomer&type=0&json=&id=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("停用客户成功!");
                        } else {
                            alert(str);
                            return;
                        }
                        //RefreshData();
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}
//转为准备客户
var fnReadyCustomer = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    if (vid != "") {
        if (confirm("您确定将该客户转为准备客户?")) {
            var SaveUrl = Url + "?r=" + Math.random() + "&action=procedure&entity=CrmCustomerReady&json=&id=" + vid;
            YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
                success: function (res) {
                    try {
                        var str = res.responseText;
                        if (str == "1") {
                            alert("操作成功!");
                            //RefreshData();
                        } else {
                            alert(str);
                            return;
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}

//网址列格式化
var formatUrl = function (elCell, oRecord, oColumn, sData) {
    var v = oRecord.getData("CompanyNetting");
    if (v.indexOf("http") > -1) {
        elCell.innerHTML = "<a href='" + v+ "' target='_blank'>" + sData + "</a>";
    }
    else {
        elCell.innerHTML = "<a href='http://" + v + "' target='_blank'>" + sData + "</a>";
    }
}
//修改信息专员
var fnModifyUserId = function () {
    var recordID = myDataTable.getSelectedRows()[0];
    var record = myDataTable.getRecord(recordID);
    var vid = record.getData(const_entityID);
    var SaveUrl = Url + "?r=" + Math.random() + "&action=check&entity=CrmCustomerCheck&json=&id=" + vid;
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "1") {
                    var vname = record.getData("UName");
                    document.getElementById("EditUIWindow").src = "../Crm/Customer/ModifyUserId.aspx?r=" + Math.random() + "&entity=CrmCustomer&id=" + vid + "&n=" + encodeURI(vname);
                    YAHOO.example.container.dialog1.show();
                }
                else {
                    alert(str);
                    //RefreshData();
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}


//保存按钮操作日志
var trySaveButtonDatLog = function (contents, vid) {
    var SaveUrl = Url + "?r=" + Math.random() + "&action=insert&entity=CrmCustomerDatLog&json=" + encodeURI(contents) + "&id=" + vid;
    YAHOO.util.Connect.asyncRequest('POST', SaveUrl, {
        success: function (res) {
            try {
                var str = res.responseText;
                if (str == "1") {
                } else {
                    alert("按钮日志数据保存失败!");
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}
//客户浏览
var fnCustomerList = function () {
    //document.getElementById("EditUIWindow").src = "../../EditWidows/SubjectList.aspx?r=" + Math.random() + "&entity=V_CrmCustomerShow&order=AddTime";
    //YAHOO.example.container.dialog1.show();
    //document.getElementById("yui-gen7-button").style.display = "none";
    window.parent.$("#tabs").tabs('close', '客户浏览');
    var pagetitle = "客户浏览";
    parent.addTabs(pagetitle, '../../EditWidows/SubjectList.aspx?entity=V_CrmCustomerShow&order=AddTime');
    parent.NeedRefreshPage = this;
}

//格式化列
var formatIsPrevate = function (elCell, oRecord, oColumn, sData) {
    if (sData == "0") {
        elCell.innerHTML = "公海";
    } else if (sData == "1") {
        elCell.innerHTML = "私海客户";
    } else if (sData == "2") {
        elCell.innerHTML = "下订单";
    } else if (sData == "3") {
        elCell.innerHTML = "退回客户";
    } else if (sData == "4") {
        elCell.innerHTML = "准备客户";
    } else if (sData == "5") {
        elCell.innerHTML = "报废";
    } else {
        elCell.innerHTML = "";
    }
}

if (typeof ($) == "undefined") { //屏蔽右键
    parent.unContextMenu(document);
}
if (typeof (YAHOO) == "undefined") {
    parent.unContextMenu(document);
}